package com.coderside.fragment.arithmetic;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class BytesOverlap {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {

		File f = new File("d:\\a.jpg");
		FileInputStream fis = new FileInputStream(f);
		byte[] b = new byte[fis.available()];
		boolean[] bs = new boolean[b.length * 8];
		fis.read(b);
		StringBuilder sb = new StringBuilder();
		for (byte c : b) {
			int d = c & 0xff;
			String s = toBinaryString(d);
			System.out.println(s);
			sb.append(s);
		}
		System.out.println(sb);
	}

	/**
	 * All possible chars for representing a number as a String
	 */
	final static char[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

	/**
	 * Convert the integer to an unsigned number.
	 */
	private static String toUnsignedString(int i, int shift, int bit) {
		char[] buf = new char[bit];
		for (int j = 0; j < buf.length; j++) {
			buf[j] = '0';
		}
		int charPos = bit;
		int radix = 1 << shift;
		int mask = radix - 1;
		do {
			if (charPos <= 0) {
				System.err.println("max bit Exception");
				return "";
			}
			buf[--charPos] = digits[i & mask];
			i >>>= shift;
		} while (i != 0);

		return new String(buf);
	}

	private static String toBinaryString(int a) {
		String str = toUnsignedString(a, 1, 8);
		return str;
	}

	private static void byte2Bits(int by, boolean[] arr) {
		System.out.println(toBinaryString(255));
		by = by & 0xfe;
		System.out.println(by);
		by = by >> 6;
		System.out.println(by);
		by = by >> 5;
		System.out.println(by);
		by = by >> 4;
		System.out.println(by);
	}

}
